Skip to content

Link namespaced crates through root modules#158724

Open
TaKO8Ki wants to merge 4 commits into
rust-lang:mainfrom
TaKO8Ki:open-ns-link-through-root
Open

Link namespaced crates through root modules#158724
TaKO8Ki wants to merge 4 commits into
rust-lang:mainfrom
TaKO8Ki:open-ns-link-through-root

Conversation

@TaKO8Ki

@TaKO8Ki TaKO8Ki commented Jul 3, 2026

Copy link
Copy Markdown
Member

Part 2 of #152299

This is a follow-up to #153312. That PR added support for resolving a namespaced crate when the root is only an open namespace root. This PR handles the case where the root crate is also present.

For example, if rustc is given both:

--extern my_api=...
--extern my_api::utils=...

then my_api::utils::item can now resolve through the my_api::utils crate when utils is not found in the real my_api crate.

The implementation is based on the Scope-based parts of #152299, but adapted to current main after #153312. In particular, this keeps the missing-root/open-module behavior from #153312 and only extracts the existing-root linking piece from the larger prototype.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 3, 2026
@TaKO8Ki

TaKO8Ki commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 3, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jul 3, 2026
Link namespaced crates through root modules
@petrochenkov petrochenkov self-assigned this Jul 3, 2026
@rust-bors

rust-bors Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 865ecdf (865ecdfbe4eabbe37c87a8ca94acf068e7bf5bd2)
Base parent: c397dae (c397dae808f70caebab1fc4e11b3edf7e59f58c7)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (865ecdf): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This perf run didn't have relevant results for this metric.

Max RSS (memory usage)

This perf run didn't have relevant results for this metric.

Cycles

Results (secondary -2.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.4% [2.4%, 2.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.9% [-6.1%, -2.7%] 3
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 485.944s -> 487.045s (0.23%)
Artifact size: 393.37 MiB -> 393.80 MiB (0.11%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 3, 2026
@TaKO8Ki TaKO8Ki marked this pull request as ready for review July 7, 2026 15:03
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 7, 2026
Comment thread tests/ui/resolve/open-ns-1.rs Outdated
@petrochenkov

Copy link
Copy Markdown
Contributor

(I'll continue reviewing tomorrow.)

@petrochenkov

Copy link
Copy Markdown
Contributor

I don't remember what we decided regarding cases like this

// --extern foo::bar=PATH

mod foo {}

let x = foo::bar::something;

Should the module mod foo link to the open module foo from the extern option?
Or there should be an error "there's no bar in foo".

@petrochenkov

Copy link
Copy Markdown
Contributor

Suppose we do link to the open module from the item module.
Then supposedly we should follow the link in cases like

// --extern foo::bar=PATH

mod foo {
  // foo is the current module, so we go through all the 3 scopes in it including the open mod link
  // this certainly doesn't look like it should work
  let x = bar::something;                                      
}

mod other {
  use crate::foo as renamed;
  // we go through all the 3 scopes in the module to which `renamed` refers, including the open mod link
  // this also doesn't look like it should work
  let x = renamed::bar;
}

@petrochenkov

Copy link
Copy Markdown
Contributor

The problem is that the similar concerns exist if we insert the links only into "crate root" modules, like this PR does.

// --extern foo::bar=PATH

extern crate foo;

mod other {
  use crate::foo as renamed;
  // we go through all the 3 scopes in the module to which `renamed` refers, including the open mod link
  // this also doesn't look like it should work
  let x = renamed::bar;
}

At least the "current module" problem is harder to trigger (but still possible!)

// --extern foo::bar=PATH

extern crate self as foo;

// foo is the current module, so we go through all the 3 scopes in it including the open mod link
// this certainly doesn't look like it should work
let x = bar::something;

@petrochenkov

Copy link
Copy Markdown
Contributor

It's quite clear that the open mod links only need to be considered in the ScopeSet::Module mode.

Also, if the links only work in ScopeSet::Module mode, then we'll be able to backward compatibly extend the set of modules into which the links are planted (e.g. from root modules to mod foo items), so we can start with just root modules.

The cases in which the links are followed still need to be restricted somehow, to avoid the cases from #158724 (comment).
(Abandoning the model with links from modules entirely would require even more fundamental hacks to name resolution.)

@petrochenkov

Copy link
Copy Markdown
Contributor

The PR misses all the test cases that demonstrate why this feature is tricky to implement.
For example:

  • glob declaration in a module emerges from a macro, after the open mod link declaration is already available
  • non-glob declaration in a module emerges from a macro, after the open mod link declaration is already available
  • extern crate declaration emerges from a macro and adds a link to a module in which some glob or non-glob declaration with the same name already exists.
  • extern crate declaration emerges from a macro and adds a link to a module in which nothing with the same name exists

@petrochenkov

Copy link
Copy Markdown
Contributor

This needs design, and I need maybe one full day to try implementing it myself and think about the details, but I don't have much time for this, unfortunately.

@TaKO8Ki
Could you add the test cases from #158724 (comment) and #158724 (comment) for now?
Also need a test with --extern introducing open module having the name of one of the built-in crates, e.g. std.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants